/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 */

package com.xjw.service;

import lombok.Data;

@Data
public class FiveChess {

    /**
     * 维度
     */
    private int n;

    private double width;

    private double height;

    private double cellLen;

    private char currentSide = 'B';

    private char flag = ' ';

    private char[][] chess;

    public FiveChess(double width, double height, double cellLen) {
        this.width = width;
        this.height = height;
        this.cellLen = cellLen;
        chess = new char[(int) height][(int) width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                chess[i][j] = ' ';
            }
        }
    }

    void play(int x, int y) {
        //将当前的棋子放置到（x,y）
        if (chess[x][y] == ' ') {
            chess[x][y] = currentSide;
            changeSide();
        }
    }

    public void changeSide() {
        //更换下棋方
        setCurrentSide(currentSide == 'B' ? 'W' : 'B');
    }


    /**
     * 判断是否有五子连珠
     * @param row 行
     * @param col 列
     * @param chessColor 棋子颜色
     * @return false代表有五子连珠
     */
    boolean judgeGame(int row, int col, char chessColor) {
        return rowJudge(row, col, chessColor) && colJudge(row, col, chessColor) && mainDiagonalJudge(row, col,
            chessColor) && DeputyDiagonalJudge(row, col, chessColor);
    }

    private boolean rowJudge(int row, int col, char chessColor) {
        //判断一行是否五子连线
        int count = 0;
        for (int j = col; j <= width - 1; j++) {
            if (chess[row][j] != chessColor) {
                break;
            }
            count++;
        }
        for (int j = col - 1; j >= 0; j--) {
            if (chess[row][j] != chessColor) {
                break;
            }
            count++;
        }
        return count < 5;
    }

    private boolean colJudge(int row, int col, char chessColor) {
        //判断一列是否五子连线
        int count = 0;
        for (int i = row; i < height; i++) {
            if (chess[i][col] != chessColor) {
                break;
            }
            count++;
        }
        for (int i = row - 1; i >= 0; i--) {
            if (chess[i][col] != chessColor) {
                break;
            }
            count++;
        }
        return count < 5;
    }

    private boolean mainDiagonalJudge(int row, int col, char chessColor) {
        //判断主对角线是否五子连线
        int count = 0;
        for (int i = row, j = col; i < height && j < width; i++, j++) {
            if (chess[i][j] != chessColor) {
                break;
            }
            count++;
        }

        for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
            if (chess[i][j] != chessColor) {
                break;
            }
            count++;
        }
        return count < 5;
    }

    private boolean DeputyDiagonalJudge(int row, int col, char chessColor) {
        //判断副对角线是否五子连线
        int count = 0;
        for (int i = row, j = col; i >= 0 && j < width; i--, j++) {
            if (chess[i][j] != chessColor) {
                break;
            }

            count++;
        }

        for (int i = row + 1, j = col - 1; i < height && j >= 0; i++, j--) {
            if (chess[i][j] != chessColor) {
                break;
            }
            count++;
        }
        return count < 5;
    }
}